package com.example.demo2.test.service;

import com.alibaba.fastjson.JSONObject;
import com.example.demo2.test.websocket.CoinbaseWebSocket;
import com.example.demo2.test.websocket.CoinbaseWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * Description:
 *
 * @author my.miao
 * @date 2022/1/5 15:03
 */
@Slf4j
@Component
public class CoinbaseService {

    @Resource
    private ExecutorService executorService;

    private static String DEPTH = "depth";
    private static String DETAIL = "detail";
    private static String MARK_PRICE = "mark-price";
    private static Map<String,String> symbolMap = new HashMap<>();// 对应请求数据的交易对
    private static Map<String,String> subTypeMap = new HashMap<>();// 对应请求数据的类型

    @Value("${coinbase.socket.api}")
    String api;
    @Value("${coinbase.socket.api.key}")
    String api_key;

    CoinbaseWebSocket webSocket;

    public void message(String text) {
        log.info("收到消息了" + text);
    }

    public void coninbaseMarket() {
        List<String> list = new ArrayList<>();// 查询要采集的交易对
        list.add("ETH-USD");
        list.add("ETH-EUR");
        webSocket = CoinbaseWebSocket.createConnectionManage(this, api);// 创建websocket连接

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type","subscribe");
        jsonObject.put("product_ids",list);
        jsonObject.put("channel","level2");
        jsonObject.put("api_key",api_key);
        jsonObject.put("timestamp","1677527973");
        jsonObject.put("signature","00000000000000000000000000");
        webSocket.send(jsonObject.toJSONString());
    }

    private void subDepthAll(List<String> list){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("method","SUBSCRIBE");
        jsonObject.put("params",list);
        jsonObject.put("id",System.currentTimeMillis());
        subMessage(jsonObject.toJSONString());//买一卖一盘 最新价
    }

    private void subTradeAll(List<String> list){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("method","SUBSCRIBE");
        jsonObject.put("params",list);
        jsonObject.put("id",System.currentTimeMillis());
        subMessage(jsonObject.toJSONString());//交易明细
    }

    private void subMessage(String subStr){
        // 参数校验(简单的为空判断)
        if (webSocket == null) {
            throw new RuntimeException("CoinbaseWebSocket 不能为空");
        }
        if(StringUtils.isEmpty(subStr)){
            throw new RuntimeException("传递的订阅信息非法");
        }
        webSocket.send(subStr);
    }

    public void SUBSCRIBE() {
        webSocket = CoinbaseWebSocket.createConnectionManage(this,api);// 创建websocket连接
        JSONObject jsonObject = new JSONObject();
        ArrayList<String> arr = new ArrayList<>();
        arr.add("btcusdt@aggTrade");
        arr.add("btcusdt@depth");
        jsonObject.put("method", "SUBSCRIBE");
        jsonObject.put("params", arr);
        jsonObject.put("id", 1);
        subMessage(jsonObject.toJSONString());
    }

    public void LIST_SUBSCRIPTIONS() {
        webSocket = CoinbaseWebSocket.createConnectionManage(this,api);// 创建websocket连接
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("method", "LIST_SUBSCRIPTIONS");
        jsonObject.put("id", 3);
        subMessage(jsonObject.toJSONString());
    }

}
